//
//  GYZFeaturedViewController.m
//  Qyer
//
//  Created by GYz on 15/10/24.
//  Copyright © 2015年 GF. All rights reserved.
//

#import "GYZFeaturedViewController.h"
#import "BaseHeader.h"

@interface GYZFeaturedViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, assign)int page;
@end

@implementation GYZFeaturedViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.infoArr = [[NSMutableArray alloc] init];

    _page = 1;
    
    [self creatNav];
    [self afn:1];
}

-(void)creatNav
{
    UIView *navV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    navV.backgroundColor = [UIColor colorWithRed:250./255 green:250./255 blue:250./255 alpha:1];
    UILabel *titLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 25, SCREEN_WIDTH, 30)];
    titLab.text = @"精选";
    titLab.textAlignment = YES;
    [navV addSubview:titLab];
    
    UIButton *backToBut = [UIButton buttonWithType:UIButtonTypeCustom];
    backToBut.frame = CGRectMake(10, 25, 30, 30);
    [backToBut setImage:[UIImage imageNamed:@"iconfont-31fanhui1"] forState:UIControlStateNormal];
    [backToBut addTarget:self action:@selector(goBackToBefore) forControlEvents:UIControlEventTouchUpInside];
    [navV addSubview:backToBut];
    [self.view addSubview:navV];
}
-(void)goBackToBefore
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)afn:(int)page
{
    [AFN getdataWithString:[NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/mguide_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&id=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&page=%d&track_app_version=6.6.6&track_user_id=6695170&type=city&v=1", self.cid, page] block:^(id block) {

        NSDictionary *dataDic = [block objectForKey:@"data"];
        for (NSDictionary *dic in dataDic) {
            GYZFeaturedModel *model = [GYZFeaturedModel modelWithDictionary:dic];
            [self.infoArr addObject:model];
        }
        if (!self.tableView) {
            [self creatTableView];
        }
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
        
    } BLOCK:^(id BLOCK) {
    }];
}

- (void)allData
{
    __block GYZFeaturedViewController *featured = self;
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        featured.page += 1;
        [featured afn:featured.page];
    }];
}

- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self allData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.infoArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"featured";
    GYZFeaturedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[GYZFeaturedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    GYZFeaturedModel *model = self.infoArr[indexPath.row];
    [cell setValueWith:model];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT / 2;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GYZFeaturedDetailViewController *detail = [[GYZFeaturedDetailViewController alloc] init];
    GYZFeaturedModel *model = self.infoArr[indexPath.row];
    detail.cid = model.cid;
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
